<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\mill\model;

use think\Model;
use think\helper\Hash;
use think\Db;

/**
 * 工程模型
 */
class Order extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'mill_order';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //工程状态
    public static $status = [
        -1 =>     '未通过',
        0 =>      '待审核',
        1 =>      '已审核',
    ];

    //外勤审核状态
    public static $waiback_status = [
        -1 =>     '未通过',
        0 =>      '待提交',
        1 =>      '待审核',
        2 =>      '已审核',
    ];

    //内勤审核状态
    public static $back_status = [
        -1 =>     '未通过',
        0 =>      '待提交',
        1 =>      '待审核',
        2 =>      '已审核',
    ];

    //面积表审核状态
    public static $archives_status = [
        -1 =>     '未通过',
        0 =>      '待提交',
        1 =>      '待审核',
        2 =>      '已审核',
    ];

    //获取所有订单信息
    public static function getList($field='engineering',$where = ''){
        $list = Db::name('mill_order')->where($where)->column($field,'id');
        return $list;
    }

    //自定义状态标签
    public static function status_tag($status=0,$label=''){
        switch ($status) {
            case '0': $class = 'warning';break;
            case '1': $class = 'info';break;
            case '2': $class = 'success';break;
            case '-1': $class = 'primary';break;
            case '-2': $class = 'primary';break;
            default: $class  = 'default';
        }
        $label = '<span class="label label-'.$class.'">'.$label.'</span>';

        return $label;
    }

    //获取指定工程所有的档案号/批次
    public static function getOrderP($order_pid=null,$where=[]){

        if(empty($where) && empty($order_pid))return '';
        if($order_pid)$where = ['o.order_pid'=>$order_pid];

        $archives_sn = Db::name('mill_order_archives_give')
            ->alias('g')
            ->leftJoin('mill_order o','g.order_id=o.id')
            ->where($where)
            ->order('g.order_id asc')
            ->column('distinct(o.archives_sn)');
        $count = count($archives_sn);
        $archives_sn = implode('/',$archives_sn);

        $batch = Db::name('mill_order_archives_give')
            ->alias('g')
            ->leftJoin('mill_order o','g.order_id=o.id')
            ->where($where)
            ->order('g.order_id asc')
            ->column('distinct(o.batch)');
        $batch = implode('/',$batch);

        $data = ['archives_sn'=>$archives_sn,'batch'=>$batch,'count'=>$count];

        return $data;
    }

    /**
     * 分析单元格合并
     * @param $v
     * @return array
     */
    public static function tableParseCell($v)
    {
        if (preg_match('/\[(.*)\]/', $v, $matches)) {
            $cell  = str_replace($matches[0], '', $v);
            $merge = explode(':', $matches[1]);
            $result = [
                'value'   => $cell,
                'colspan' => $merge[0],
                'rowspan' => isset($merge[1]) ? $merge[1] : '',
            ];
        } else {
            $result = [
                'value'   => $v,
                'rowspan' => '',
                'colspan' => '',
            ];
        }

        return $result;
    }

    // 配置订单编号
    public static function orderSn($order_id=0){
        $order_sn = "JY".date('y')."-";
        $sort = $order_id;
        $sample_type = Db::name('mill_order_sample')->where(['order_id'=>$order_id])->value('type');
        if($sample_type == 1){
            $order_sn .= "YP";
            $sort = Db::name('mill_order')->where("order_sn LIKE '%YP%'")->order('order_sn DESC')->value('order_sn');
        }
        else{
            $sort = Db::name('mill_order')->where("order_sn NOT LIKE '%YP%'")->order('order_sn DESC')->value('order_sn');
        }
        $sort_arr = explode('-',$sort);
        if(trim($sort_arr[0],'JY') != date('y'))$sort = 0;
        else{
            $sort = $sort_arr[1];
            $sort = trim($sort,'YP');
        }

        $sort ++;

        $order_sn .= sprintf ("%03d", $sort);

        return $order_sn;
    }

}
